/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package AdvancedOptions;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.SwingUtilities;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Iterator;

import jktest.JKMainGui;

/**
 *
 * Class to allow the user to control the components on the main GUI
 * 
 * @author Royce Feng
 */
public class AdvancedOptionsGUI implements ActionListener {

    private JKMainGui parent;
    private JDialog frame = new JDialog();
    private JPanel panel = new JPanel();

    // User Selection options
    private JPanel panelSelections = new JPanel(new GridLayout(8,3));
    private ButtonGroup devices = new ButtonGroup();
    private ButtonGroup partitions = new ButtonGroup();
    private ButtonGroup ethernet = new ButtonGroup();
    private ButtonGroup sound = new ButtonGroup();
    private ButtonGroup openOffice = new ButtonGroup();
    private ButtonGroup software = new ButtonGroup();
    private ButtonGroup floppy = new ButtonGroup();
    private ButtonGroup extendedSoftware = new ButtonGroup();

    // Control components
    private JPanel controlSelections = new JPanel();
    private ButtonGroup showMessage = new ButtonGroup();

    private HashMap<String,ButtonGroup> componentMap = new HashMap<String,ButtonGroup>();

    private class KeyStalker implements KeyListener {
        public void keyReleased( KeyEvent e ) {}
        public void keyPressed( KeyEvent e ) {
            switch ( e.getKeyCode() ) {
                case 114: //F3
                case 27: // ESC
                    frame.setVisible(!frame.isVisible());
                    break;
                default:
                    break;
            }
        }
        public void keyTyped( KeyEvent e ) {}
    }

    private void ctrlCmpInit() {
        controlSelections.setLayout( new BoxLayout(controlSelections, BoxLayout.Y_AXIS) );
        controlSelections.setBorder(BorderFactory.createTitledBorder("Control Components"));
        JPanel showMessagePanel = new JPanel( new GridLayout(1,3) );
        showMessagePanel.add( new JLabel("Show Messages") );
        JRadioButton opYes = new JRadioButton("On");
        opYes.setSelected(true);
        JRadioButton opNo = new JRadioButton("Off");
        opYes.addKeyListener(new KeyStalker());
        opNo.addKeyListener(new KeyStalker());
        opYes.setActionCommand("On");
        opNo.setActionCommand("Off");
        showMessagePanel.add(opYes);
        showMessagePanel.add(opNo);
        showMessage.add(opYes);
        showMessage.add(opNo);
        controlSelections.add(showMessagePanel);
    }

    /**
     *
     * Main constructor. Sets up the main GUI components (radio buttons and labels).
     *
     * @param _parent
     */
    public AdvancedOptionsGUI ( JKMainGui _parent ) {
        parent = _parent;
        frame.setTitle("Advanced Options");
        frame.addKeyListener(new KeyStalker());
        frame.setContentPane(panel);
        frame.setResizable(false);
        frame.setVisible ( false );
        frame.setDefaultCloseOperation ( JDialog.HIDE_ON_CLOSE );
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        componentMap.put("devices", devices);
        componentMap.put("partitions", partitions);
        componentMap.put("ethernet", ethernet);
        componentMap.put("sound", sound);
        //componentMap.put("openOffice", openOffice);
        componentMap.put("software", software);
        componentMap.put("floppy", floppy);
        componentMap.put("extended", extendedSoftware);
        TitledBorder title;
        title = BorderFactory.createTitledBorder("Enable/Disable Tests");
        panelSelections.setBorder(title);
        Iterator<String> iter = componentMap.keySet().iterator();
        while ( iter.hasNext() ) {
            String keyname = iter.next();
            ButtonGroup bg = componentMap.get(keyname);
            JRadioButton rb1 = new JRadioButton("On");
            JRadioButton rb2 = new JRadioButton("Off");
            rb1.addKeyListener(new KeyStalker());
            rb2.addKeyListener(new KeyStalker());
            if ( parent.panelEnabled(keyname) ) {
                rb1.setSelected(true);
            } else {
                rb2.setSelected(true);
            }
            rb1.setActionCommand("On");
            rb2.setActionCommand("Off");
            bg.add(rb1);
            bg.add(rb2);
            panelSelections.add(new JLabel(keyname));
            panelSelections.add(rb1);
            panelSelections.add(rb2);
        }
        panel.add(panelSelections);
        ctrlCmpInit();
        panel.add(controlSelections);
        JButton setValues = new JButton("Set preferences");
        panel.add(setValues);
        setValues.addActionListener(this);
        frame.pack();
    }

    /**
     *
     * Swaps the current visibility (if true, then false, if false then true)
     *
     */
    public void toggleVisible() {
        frame.setVisible(!frame.isVisible());
    }

    /**
     *
     * Called when the button is clicked. This will go through all the different hashed
     * ButtonGroups and see if a panel has been enabled or disabled, and then tell the
     * main GUI to enable/disable the panel.
     *
     * @param e The ActionEvent
     */
    public void actionPerformed ( ActionEvent e ) {
        Iterator<String> iter = componentMap.keySet().iterator();
        while ( iter.hasNext() ) {
            String keyname = iter.next();
            ButtonGroup bg = componentMap.get(keyname);
            parent.togglePanel(keyname,bg.getSelection().getActionCommand().equals("On"));
        }
        boolean messageState = showMessage.getSelection().getActionCommand().equals("On");
        if ( !messageState )
            JOptionPane.showMessageDialog( null, "You have chosen to turn off messaging. Please note that this only works " +
                    "for non-error messages.", "Turn off messaging", JOptionPane.INFORMATION_MESSAGE );
        parent.setMessageState(messageState);
    }

    /**
     *
     * Reskins and packs the GUI
     *
     */
    public void pack() {
        SwingUtilities.updateComponentTreeUI(frame);
        frame.pack();
    }
    
}
